//
//  MessageViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/21.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "MessageViewController.h"
#import "ScheduleViewController.h"
#import "FocusedHouseViewController.h"
#import "RoomMateTeamViewController.h"

#import "EmptyView.h"

#import "ACMacros.h"
#import "Utility.h"
#import "Constants.h"

#import <SDWebImage/UIImageView+WebCache.h>
#import <MobClick.h>

@interface MessageViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation MessageViewController {
    NSMutableArray *messages;
    
    EmptyView *emptyView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureNavigationBar];
    [self configureTableView];
    [self configureEmptyView];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    messages = [Utility getObjectForKey:kMessageArray];
    if (self.tableView) {
        [self.tableView reloadData];
    }
    [MobClick beginLogPageView:@"消息页"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [MobClick endLogPageView:@"消息页"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Configuring the view’s layout behavior
- (void)configureNavigationBar {
    self.title = @"消息中心";
    self.navigationItem.hidesBackButton = true;
    UIImage *backImage = [[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:@selector(clickToPopCancel)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

- (void)configureTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height - 20)];
    [self.view addSubview:self.tableView];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
}

- (void)configureEmptyView {
    emptyView = [[EmptyView alloc]init];
    emptyView = [[[NSBundle mainBundle]loadNibNamed:@"EmptyView" owner:nil options:nil] lastObject];
    [emptyView setFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height - 64)];
    [emptyView.emptyImage setImage:[UIImage imageNamed:@"OX_message156-126"]];
    [emptyView.titleLabel setText:@"暂无推送消息"];
}

#pragma mark - Triggering Methods
- (void)clickToPopCancel {
    [self.navigationController popToRootViewControllerAnimated:true];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (messages.count == 0) {
        [self.tableView addSubview:emptyView];
    }
    return messages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    id userInfo = messages[indexPath.row];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(10, 66.5, Main_Screen_Width - 20, 0.5)];
    [line setBackgroundColor:RGBACOLOR(240, 240, 240, 1)];
    [cell addSubview:line];
    [cell.textLabel setText:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]];
    [cell.detailTextLabel setText:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]];
    [cell.detailTextLabel setTextColor:RGBACOLOR(119, 119, 119, 1)];
    if ([[userInfo objectForKey:@"type"] isEqualToString:@"teammate"]) {
        cell.tag = 0;
        [cell.imageView setImage:[UIImage imageNamed:@"new_roomie"]];
    } else if ([[userInfo objectForKey:@"type"] isEqualToString:@"teamApply"]) {
        cell.tag = 1;
        [cell.imageView setImage:[UIImage imageNamed:@"new_add"]];
    } else if ([[userInfo objectForKey:@"type"] isEqualToString:@"schedule"]) {
        cell.tag = 2;
        [cell.imageView setImage:[UIImage imageNamed:@"new_schedule"]];
    } else if ([[userInfo objectForKey:@"type"] isEqualToString:@"favorite"]) {
        cell.tag = 3;
        [cell.imageView setImage:[UIImage imageNamed:@"new_like"]];
    } else {
        cell.tag = 4;
        [cell.imageView setImage:[UIImage imageNamed:@"new_message"]];
    }
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 67;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 7;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelected:true];
    switch (cell.tag) {
        case 0:
            break;
        case 1:
        {
            RoomMateTeamViewController *roommateVC = [[RoomMateTeamViewController alloc]init];
            [self.navigationController pushViewController:roommateVC animated:true];
        }
            break;
        case 2:
        {
            ScheduleViewController *scheduleVC = [[ScheduleViewController alloc]init];
            [self.navigationController pushViewController:scheduleVC animated:true];
        }
            
            break;
        case 3:
        {
            FocusedHouseViewController *focusVC = [[FocusedHouseViewController alloc]init];
            [self.navigationController pushViewController:focusVC animated:true];
            
        }
            break;
        case 4:
            break;
            
        default:
            break;
    }
    [cell setSelected:false];
}

@end
